<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta charset="utf-8" />
<meta name="description" content="Mailbox with some customizations as described in docs" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
<%@ include file="/include/main_include.jsp"%>
<script type="text/javascript" src="${path}/static/myjs/plugins/grsunChooseWindow.js?v=10"></script>
</head>
<body class="easyui-layout">
	<div data-options="region:'north',border:false" style="height:42px;">
		<div class="grsun-toolbar">
				<button type="button" id="add" class="btn btn btn-toolbar btn-white btn-info no-border auth-hidden">
					<i class="ace-icon fa fa-plus blue bigger"></i>
					增加
				</button>
				<button type="button" id="edit" class="btn btn btn-toolbar btn-white btn-info no-border auth-hidden">
					<i class="ace-icon fa fa-edit orange bigger"></i>
					修改
				</button>
				<button type="button" id="del" class="btn btn btn-toolbar btn-white btn-info no-border auth-hidden">
					<i class="ace-icon fa fa-remove red bigger"></i>
					删除
				</button>
		</div>
	</div>
	<div data-options="region:'center',border:false">
		<table id="data_table"></table>
	</div>  
	<script type="text/javascript">
		var searchParams;
		$(function(){
			//添加按钮事件
			$('#add').on('click', function() {
				openedit('');
			});
			//修改按钮事件
			$('#edit').on('click', function() {
				var node = dataTable.datagrid('getSelected');
				if (node) {
					openedit(node.roleId);
				} else {
					layer.msg('请选择一条数据！', {
						time : 1000
					})
				}
			});
			//删除按钮事件
			$('#del').on('click', function() {
				del();
			});
			 
		});
		//Table表格数据源
		var dataTable = $('#data_table').datagrid(BasicData.createDatagrid({  
			url : rootpath + '/sysRole/list.do',
			frozenColumns:[[
				{field:'ck',checkbox:true},
				{title:'roleId',field:'roleId',hidden:true}
			]],
			columns:[[
				{field:'name',title:'角色名称',width:'20%',sortable:true,align:'center'},
				{field:'roleCode',title:'角色编码',width:'20%',sortable:true,align:'center'},
				{field:'menuRights',title:'资源权限',width:'10%',align:'center',
					formatter: function(value,row,index){
						var btn = '<button type="button" id="menu_qx" onclick="saveqx(\''+row.roleId+'\',\'menu_qx\')" class="btn btn btn-toolbar btn-white btn-info no-border auth-hidden"><i class="ace-icon fa fa-gear green bigger"></i>资源</button>';
						return btn;
					}
				}
			]],
			onDblClickRow:function(rowIndex, rowData){
				
			}
		}));  
		//删除
		function del() {
			var node = dataTable.datagrid('getSelected');
			if (node) {
				layer.confirm('您确定要删除吗？', {
					btn : [ '确定', '取消' ]
				}, function() {
					$.uitools.showLoading();//打开处理中遮罩层
					$.ajax({
						cache : true,
						type : "POST",
						url : rootpath + '/sysRole/delete.do',
						data : {
							roleId : node.roleId
						},
						async : false,
						error : function(request) {
							$.uitools.hideLoading();//关闭处理中遮罩层
							layer.msg('系统异常！', {
								icon : 2,
								time : 1000
							})
						},
						success : function(data) {
							$.uitools.hideLoading();//关闭处理中遮罩层
							if (data.code==0){
								layer.msg('删除成功！', {
									icon : 1,
									time : 1000
								}, function() {
									dataTable.datagrid('reload');
								})
							}else {
								layer.msg('删除异常！', {
									icon : 2,
									time : 1000
								})
							}
						}
					});
				}, function() {
				});
			} else {
				layer.msg('请选择一条数据！', {
					time : 1000
				})
			}
		}

		//编辑打开窗体
		function openedit(ROLE_ID){
			layer.open({
				title : '信息',
				type : 2,
				btn : ['保存','关闭'],
				yes : function(index,layero) {
					var iframeWin = window[layero.find('iframe')[0]['name']];
					iframeWin.$("#dataform").submit();
				},
				btn2 : function(index, layero) {
				},
				area : [ '50%', '50%' ],
				content : rootpath + '/sysRole/toEdit.do?roleId='+ROLE_ID
			});
		}
		//保存权限
		function saveqx(ROLE_ID,TYPE){
			grsunChooseWindow.menuChooseTreeWindow.show(window,function(result) {
				var MENUIDS = "";
				var tmpNode;
				for(var i =0;i<result.length;i++){
					tmpNode = result[i];
					if(i!=result.length-1){
						MENUIDS += tmpNode.id+",";
					}else{
						MENUIDS += tmpNode.id;
					}
				}
				$.uitools.showLoading();//打开处理中遮罩层
					$.ajax({
						cache : true,
						type : "POST",
						url : rootpath + '/sysRole/saveqx.do',
						data :{roleId:ROLE_ID,TYPE:TYPE,MENUIDS:MENUIDS},
						async : false,
						error : function(request) {
							$.uitools.hideLoading();//关闭处理中遮罩层
							layer.msg('系统异常！', {
								icon : 2,
								time : 1000
							}, function() {
								parent.layer.close(index);
							})
						},
						success : function(data) {
							$.uitools.hideLoading();//关闭处理中遮罩层
							if (data.code==0) {
								layer.msg('提交成功！', {
									icon : 1,
									time : 1000
								}, function() {
									parent.layer.close(index);
									//刷新列表
									parent.dataTable.datagrid('reload');
								})
							} else {
								layer.msg('提交异常！', {
									icon : 2,
									time : 1000
								}, function() {
									parent.layer.close(index);
								})
							}
						}
					});
			},rootpath+"/sysRole/toChooseTree.do?roleId="+ROLE_ID+"&TYPE="+TYPE);
		}
	</script>
</body>
</html>